<?php


namespace app\admin\controller;

use app\common\model\Goods as G;
use app\common\model\GoodsCate as GC;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;
class Goods extends Base
{
    public function index() {
        $pid = Request::param('pid');
        //类别pid
        $where = Request::param('where', []);
        $where = array_filter($where);
        $keyword = Request::param('keyword');
        $list = G::where($where)
            ->where(function ($query) use ($keyword) {
                if (!empty($keyword)) {
                    $query->whereLike('goods_name', "%$keyword%");
                }
            }
            )
            ->order('sort asc')
            ->paginate(10, false, ['query' => request()->param()]);
        $page = $list->render();
        $view = [
            'keyword' => $keyword,
            'page' => $page,
            'list' => $list,
            'pid' => $pid,
        ];
        View::assign($view);
        return View::fetch();
    }

    public function add() {
        //查询源码
        $attrinfo = Db::name('attr')->where('group_status',1)->order('group_sort')->select()->toArray();
        foreach ($attrinfo as $k => $v) {
            $attrg = Db::name('attrg')->where('group_id',$attrinfo[$k]['group_id'])->select()->toArray();
            if($attrg) {
                foreach ($attrg as $keys => $values) {
                    $attrinfo[$k]['son'][$keys]['type_name']=$attrg[$keys]['attrg_name'];
                    $attrinfo[$k]['son'][$keys]['type_id']=$attrg[$keys]['attrg_id'];
                }
            }
        }
        //dump($attrinfo);die;
        //查询布局
        $layouinfo = Db::name('attr')->where('taskid',2)->select()->toArray();
        foreach ($layouinfo as $k => $v) {
            $attrg = Db::name('attrg')->where('group_id',$layouinfo[$k]['group_id'])->select()->toArray();
            if($attrg) {
                foreach ($attrg as $keys => $values) {
                    $layouinfo[$k]['son'][$keys]['type_name']=$attrg[$keys]['attrg_name'];
                    $layouinfo[$k]['son'][$keys]['type_id']=$attrg[$keys]['attrg_id'];
                }
            }
        }
        //获取商品列表
        $goodscate = GC::field('id as gcate_id,goodscatename,image,parentid')
            ->order('sort ASC,id ASC')
            ->select();
        $goodscate = goodscate_tree($goodscate);
        $view = [
            'attrinfo'=>$attrinfo,
            'layouinfo'=>$layouinfo,
            'goodscate'=>$goodscate,
            'info' => null
        ];
        View::assign($view);
        return View::fetch();
    }

    public function addPost(){

    }
}